################################################################################
# The Sandbox Libraries Makefile                                               #
#                                                                              #
# Copyright (C) 2004-2009, 2011-2013 LIU Yu, pineapple.liu@gmail.com           #
# All rights reserved.                                                         #
#                                                                              #
# Redistribution and use in source and binary forms, with or without           #
# modification, are permitted provided that the following conditions are met:  #
#                                                                              #
# 1. Redistributions of source code must retain the above copyright notice,    #
#    this list of conditions and the following disclaimer.                     #
#                                                                              #
# 2. Redistributions in binary form must reproduce the above copyright notice, #
#    this list of conditions and the following disclaimer in the documentation #
#    and/or other materials provided with the distribution.                    #
#                                                                              #
# 3. Neither the name of the author(s) nor the names of its contributors may   #
#    be used to endorse or promote products derived from this software without #
#    specific prior written permission.                                        #
#                                                                              #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  #
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE    #
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE   #
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE     #
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR          #
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF         #
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS     #
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN      #
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)      #
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE   #
# POSSIBILITY OF SUCH DAMAGE.                                                  #
################################################################################

prefix ?= @prefix@
exec_prefix ?= @exec_prefix@
includedir ?= @includedir@
libdir ?= @libdir@
LIBRARY = @PACKAGE_NAME@
VERSION = @PACKAGE_VERSION@
RELEASE = @PACKAGE_RELEASE@

AR = @AR@ rcs
AWK = @AWK@
LN = @LN_S@ -f
TAR = @TAR@
MKDIR = @MKDIR_P@
INSTALL = @INSTALL@ -D
INSTALL_DATA = @INSTALL_DATA@ -D

# build options
CC = @CC@
CFLAGS = @CFLAGS@
LDFLAGS = @LDFLAGS@ -shared
LIBS = @LIBS@

# buid sources and targets
$(LIBRARY)_hdr = src/platform.h src/sandbox.h src/internal.h src/config.h
$(LIBRARY)_src = src/platform.c src/sandbox.c src/internal.c
$(LIBRARY)_obj = $($(LIBRARY)_src:.c=.@OBJEXT@)
$(LIBRARY)_sdist = $($(LIBRARY)_hdr:config.h=config.h.in) \
                   $($(LIBRARY)_src:internal.c=internal.awk) \
                   $(LIBRARY).spec $(LIBRARY).pc Makefile.in Doxyfile.in \
                   configure install-sh README COPYING CHANGES
$(LIBRARY)_bdist = lib$(LIBRARY).so lib$(LIBRARY).so.$(VERSION) lib$(LIBRARY).a

# build branches
.PHONY: all clean sdist install

all: $($(LIBRARY)_bdist) src/$(LIBRARY).h

$($(LIBRARY)_obj): $($(LIBRARY)_hdr) $($(LIBRARY)_src) Makefile

lib$(LIBRARY).so : lib$(LIBRARY).so.$(VERSION)
	$(LN) lib$(LIBRARY).so.$(VERSION) lib$(LIBRARY).so

lib$(LIBRARY).so.$(VERSION) : $($(LIBRARY)_obj)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $(@) $($(LIBRARY)_obj) $(LIBS)

lib$(LIBRARY).a: $($(LIBRARY)_obj)
	$(AR) $(@) $($(LIBRARY)_obj)

$(prefix) dist:
	$(MKDIR) $(@)

clean:
	rm -f src/internal.c src/config.h config.log config.status Makefile Doxyfile
	rm -f $($(LIBRARY)_obj) $($(LIBRARY)_bdist)
	if test -e $(LIBRARY).pc.in && ! test $(LIBRARY).pc -nt $(LIBRARY).pc.in; then rm -f $(LIBRARY).pc; fi
	if test -e $(LIBRARY).spec.in && ! test $(LIBRARY).spec -nt $(LIBRARY).spec.in; then rm -f $(LIBRARY).spec; fi

sdist: dist $($(LIBRARY)_sdist)
	$(TAR) --dereference -czf dist/lib$(LIBRARY)-$(VERSION).tar.gz $($(LIBRARY)_sdist)
	rm -rf dist/lib$(LIBRARY)-$(VERSION)
	$(MKDIR) dist/lib$(LIBRARY)-$(VERSION)
	$(TAR) -C dist/lib$(LIBRARY)-$(VERSION) -xzf dist/lib$(LIBRARY)-$(VERSION).tar.gz
	$(TAR) -C dist -czvf dist/lib$(LIBRARY)-$(VERSION).tar.gz lib$(LIBRARY)-$(VERSION)
	rm -rf dist/lib$(LIBRARY)-$(VERSION)

install: $(prefix) $($(LIBRARY)_bdist) src/$(LIBRARY).h
	$(INSTALL_DATA) src/$(LIBRARY).h $(includedir)/$(LIBRARY).h
	$(INSTALL_DATA) src/internal.h $(includedir)/$(LIBRARY)-dev.h
	$(INSTALL_DATA) $(LIBRARY).pc $(libdir)/pkgconfig/lib$(LIBRARY).pc
	$(INSTALL_DATA) lib$(LIBRARY).a $(libdir)/lib$(LIBRARY).a
	$(INSTALL) lib$(LIBRARY).so.$(VERSION) $(libdir)/lib$(LIBRARY).so.$(VERSION)
	$(LN) lib$(LIBRARY).so.$(VERSION) $(libdir)/lib$(LIBRARY).so

Makefile: Makefile.in
	./config.status

src/config.h: src/config.h.in
	./config.status

config.status: configure
	./config.status --recheck

%.c: %.awk
	$(AWK) -f $(<) $($(LIBRARY)_hdr) $($(LIBRARY)_src) > $(@)

%.@OBJEXT@: %.c
	$(CC) $(CFLAGS) -c -o $(@) $(<)

